﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using SilverApp.View;

namespace SilverApp.ViewModel
{
    public class AuthViewModel : ViewModelBase
    {
        public bool                             connected = false;
        private LiveService.User                currentUser = null;
        private ICommand                        authenticateCommand;
        private MainPageViewModel               mparent;
        private String errmsg = "";
        //private LiveService.LServiceClient      sclient;

        public AuthViewModel(MainPageViewModel parent)
        {
            mparent = parent;
            currentUser = new LiveService.User();
            authenticateCommand = new RelayCommand(Authenticate, CanAuthenticate);
        }

        public LiveService.User CurrentUser
        {
            get { return this.currentUser; }
            set { this.currentUser = value; }
        }

        public String CurrentUserLogin
        {
            get
            {
                   return this.currentUser.login;
            }   
            set { this.currentUser.login = value; }
        }

        public String CurrentUserPassword
        {
            get { return this.currentUser.password; }
            set { this.currentUser.password = value; }
        }

        public ICommand AuthenticateCommand
        {
            get { return this.authenticateCommand; }
            set { this.authenticateCommand = value; }
        }

        private void Authenticate(object parameter)
        {
            try
            {                
                this.mparent.Message = "Authenticate started";
                this.Sclient.authenticateUserCompleted += new EventHandler<LiveService.authenticateUserCompletedEventArgs>(AuthenticateCompleted);
                this.Sclient.authenticateUserAsync(this.currentUser);
             }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private bool CanAuthenticate(object parameter)
        {
            return true;
        }

        public void AuthenticateCompleted(object sender, LiveService.authenticateUserCompletedEventArgs e)
        {
            try
            {
                this.connected = e.Result;
                if (e.Result == true)
                {
                    this.Sclient.getUserProfileByLoginCompleted += new EventHandler<LiveService.getUserProfileByLoginCompletedEventArgs>(LoadUserCompleted);
                    this.Sclient.getUserProfileByLoginAsync(this.currentUser.login);
                }
                else {
                    this.ErrorAuth = "Invalid credentials !";
                    // this.Sclient.getLastErrorCompleted += new EventHandler<LiveService.getLastErrorCompletedEventArgs>(Sclient_getLastErrorCompleted);
                   // this.Sclient.getLastErrorAsync();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void Sclient_getLastErrorCompleted(object sender, LiveService.getLastErrorCompletedEventArgs e)
        {
            if (e.Result.Length <= 0)
                mparent.Message = "result <= 0";
            mparent.Message = e.Result;
        }

        public void LoadUserCompleted(object sender, LiveService.getUserProfileByLoginCompletedEventArgs e)
        {
            try
            {
                this.currentUser = e.Result;
                this.mparent.RegisterUser(this.currentUser);
                this.mparent.AuthWin.Close();
                this.mparent.RegisterNextView("Home");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public String ErrorAuth
        {
            get { return this.errmsg; }
            set { this.errmsg = value; OnPropertyChanged("ErrorAuth"); }
        }
    }
}
